package com.nlp.trie;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileRW {
	
	public String read(String filePath) {
		StringBuilder msg = new StringBuilder();
		File file = new File(filePath);
		InputStreamReader isr = null;
		try {
			isr = new InputStreamReader(new FileInputStream(file));
			char[] data = new char[2048];
			int len = isr.read(data);
			while (len > 0) {
				msg.append(data, 0, len);
				len = isr.read(data);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return msg.toString();
	}

	public void write(String path, String msg) {
		File file = new File(path);
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			byte[] b = msg.getBytes();
			fos.write(b);
			fos.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
